package com.penghk.earth.Controller;


import com.penghk.earth.job.FactoryJob;
import com.penghk.earth.job.MyJobListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;
import org.quartz.impl.matchers.KeyMatcher;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.Oneway;
import java.util.Objects;

@RestController
@RequestMapping("/quartz")
public class QuartzController {
    private static final Log logger = LogFactory.getLog(QuartzController.class);

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Autowired
    JobFactory jobFactory;

    @Autowired
    JobListener jobListener;

    @RequestMapping(value = "start", method = RequestMethod.POST)
    public String start() throws SchedulerException {
        //获取单例
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        scheduler.getListenerManager().addJobListener(jobListener , KeyMatcher.keyEquals(new JobKey("taskname" , "group1")));
        scheduler.setJobFactory(jobFactory);

        TriggerKey triggerKey = TriggerKey.triggerKey("taskname" , "group1");
        try {
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if (Objects.isNull(trigger)) {
                JobDetail jobDetail = JobBuilder.newJob(FactoryJob.class).withIdentity(triggerKey.getName() , triggerKey.getGroup()).build();
                jobDetail.getJobDataMap().put("name" , "tony");
                jobDetail.getJobDataMap().put("count" , 0);

                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
                trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey.getName() , triggerKey.getGroup()).withSchedule(scheduleBuilder).build();
                scheduler.scheduleJob(jobDetail , trigger);
            }else {
                // Trigger已存在，那么更新相应的定时设置
                //表达式调度构建器
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");

                //按新的cronExpression表达式重新构建trigger
                trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
                        .withSchedule(scheduleBuilder).build();

                //按新的trigger重新设置job执行
                scheduler.rescheduleJob(triggerKey, trigger);
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "success start job";
    }

    @RequestMapping(value = "pause", method = RequestMethod.POST)
    public String pause() {
        try {
            //停止一个正在执行的任务
            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey("taskname" , "group1");
            scheduler.pauseTrigger(triggerKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "stop job success";
    }

    @RequestMapping(value = "resume", method = RequestMethod.POST)
    public String resume() {
        try {
            //恢复一个正在执行的任务
            Scheduler scheduler = schedulerFactoryBean.getScheduler();
            TriggerKey triggerKey = TriggerKey.triggerKey("taskname" , "group1");
            scheduler.resumeTrigger(triggerKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "resume succeed";
    }

    @RequestMapping(value = "stop", method = RequestMethod.POST)
    public String stop() throws SchedulerException {
        //恢复一个正在执行的任务
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        TriggerKey triggerKey = TriggerKey.triggerKey("taskname" , "group1");
        scheduler.unscheduleJob(triggerKey);
        return "stop success";
    }

}
